# -*- coding: utf-8 -*-

import redis.asyncio as async_redis
from langgraph.checkpoint.redis import AsyncRedisSaver

from config.DBConfig import REDIS_HOST, REDIS_PORT, REDIS_DB
from core.base import chat_llm
from mcp_client.MultiClinet import getMCPTools
from util.tools import searxng_search, get_weather, milvus_search
from langgraph.prebuilt import create_react_agent






async def create_custom_react_agent():
    _agent_instance = None
    if _agent_instance is None:
        tools = [get_weather, searxng_search, milvus_search, milvus_search]
        mcp_tools = await getMCPTools()
        tools.extend(mcp_tools)
        llm = chat_llm()
        # 创建 LimitedMemorySaver，限制为 5 轮
        # memory = LimitedMemorySaver()
        redis_client = async_redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)

        memory = AsyncRedisSaver(redis_client= redis_client,ttl={"default_ttl":60*24} )
        await memory.asetup()
        _agent_instance = create_react_agent(llm, tools, checkpointer=memory).with_config({"recursion_limit": 25})
    return _agent_instance



